package cn.bugstack.types.design.framework.tree;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import lombok.Getter;
import lombok.Setter;

/**
 * @author hsj
 * @date 2025/2/13 15:56
 */
public abstract class AbstractMultiThreadStrategyRouter<T, D, R> implements StrategyMapper<T, D, R>, StrategyHandler<T, D, R>   {

	@Getter
	@Setter
	protected StrategyHandler<T, D, R> defaultStrategyHandler = StrategyHandler.DEFAULT;


	public R router(T requestParameter, D dynamicContext) throws ExecutionException, InterruptedException, TimeoutException {
		StrategyHandler<T, D, R> strategyHandler = get(requestParameter, dynamicContext);
		if (null != strategyHandler) return strategyHandler.apply(requestParameter,dynamicContext);
		return strategyHandler.apply(requestParameter,dynamicContext);
	}


	@Override
	public R apply(T requestParameter, D dynamicContext) throws ExecutionException, InterruptedException, TimeoutException {
		multiThread(requestParameter,dynamicContext);
		return doApply(requestParameter,dynamicContext);
	}

	protected abstract R doApply(T requestParameter, D dynamicContext)
		throws ExecutionException, InterruptedException, TimeoutException;

	@Override
	public StrategyHandler<T, D, R> get(T requestParameter, D dynamicContext) {
		return null;
	}

	protected abstract void multiThread(T requestParameter, D dynamicContext)
		throws ExecutionException, InterruptedException, TimeoutException;

}
